/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO_BUS;

import DataObject.MyColumn;
import DataObject.MyData;
import DataObject.MyRowData;
import java.util.Vector;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

/**
 *
 * @author MinhNhat
 */
public class MySelectResult {
    Vector Data;//Mang cac element
    public MySelectResult()
    {
        Data = new Vector();
    }
    
    public void Print()
    {
       Element ele =(Element)Data.get(0);
       System.out.println(ele.getAttribute("LastName"));
    
    }
    
    public boolean CheckCondition(Vector Conditions, Vector Vars)
    {
        if(MyExpression.GetTheTruth(Conditions,Data,Vars)==0)
        {
            return false;
        }
        return true;
    }
    
    public MyRowData GetValue(Vector Tables,Vector FromTables, Vector Properties, Vector GroupByColumns)
    {
        MyRowData Row= new MyRowData();
        for(int i=0;i<Properties.size();i++)
        {
            MyPropertySelect Property = (MyPropertySelect)Properties.get(i);
            String property= Property.NameColumn;
            String propertyCandidate = SelectSentence.GetColumnInSpecialProperties(property);
            if(!propertyCandidate.isEmpty())
            {
                property = propertyCandidate;
            }
 
            int indexOfData= GetIndexTableFromProperties(FromTables,property);
            if(indexOfData!=-1)
            {
                Element ele = (Element)Data.get(indexOfData);
                String[] partsProperty = property.replace(".", " ").split(" ");
                String nameColumn,nameTable;
                if(partsProperty.length==2)
                {
                    nameColumn= partsProperty[1];
                    nameTable = partsProperty[0];
                }
                else
                {
                    nameColumn= partsProperty[2];
                    nameTable = partsProperty[0];
                
                }
                
                if(nameColumn.compareTo("*")==0)
                {
                    NamedNodeMap list= ele.getAttributes();
                    for(int j=0;j<list.getLength();j++)
                    {
                        Node node= (Node)list.item(j);
                        MyData Data = GetData(ele,Tables,nameTable,node.getNodeName());
                        Row.Datas.add(Data);
                    }
                }
                else
                {                                    
                    MyData Data= GetData(ele,Tables,nameTable,nameColumn);
                    
                    if(!Property.NewNameColumn.isEmpty())
                    {
                       Data.Column =  Property.NewNameColumn;
                    }                  
                    Row.Datas.add(Data);
                }
            }
        }
        
        for(int i=0;i<GroupByColumns.size();i++)
        {
            String GroupByColumn = (String)GroupByColumns.get(i);
            int indexOfData= GetIndexTableFromProperties(FromTables,GroupByColumn);
            if(indexOfData!=-1)
            {
                Element ele = (Element)Data.get(indexOfData);
                String[] partsProperty = GroupByColumn.replace(".", " ").split(" ");
                String nameColumn,nameTable;
                if(partsProperty.length==2)
                {
                    nameColumn= partsProperty[1];
                    nameTable = partsProperty[0];
                }
                else
                {
                    nameColumn= partsProperty[2];
                    nameTable = partsProperty[0];
                
                }
                               
                MyData Data= GetData(ele,Tables,nameTable,nameColumn);
                for(int j=0;j<Row.Datas.size();j++)
                {
                    MyData DataInRow = (MyData)Row.Datas.get(j);
                    if(Data.Column.compareTo(DataInRow.Column)==0)
                    {
                        Data = null;
                        break;
                    }
                }
                if(Data!=null)
                {
                    Row.Datas.add(Data);
                }
            }
        }
        return Row;
    }
    
    public MyData GetData(Element ele,Vector Tables,String nameTable,String nameColumn)
    {
        String Value = ele.getAttribute(nameColumn);       
        MyColumn Column= MyTable.GetColumn(Tables, nameTable, nameColumn);        
        MyData Data= new MyData(Column,Value);                
        return Data;
    }
    
    public int GetIndexTableFromProperties(Vector FromTables,String Property)
    {
        for(int i=0;i<FromTables.size();i++)
        {
            String fromTable= (String)FromTables.get(i);
            String nameTableWithProperties;
            if(Property.contains("."))
            {
                nameTableWithProperties= Property.substring(0,Property.lastIndexOf("."));  
            }
            else
            {
                nameTableWithProperties= Property;
            }
           
            
            if(fromTable.compareTo(nameTableWithProperties)==0)
            {
                return i;
            }
        }
        return -1;
    }
    
   
}
